<template>
    <div class="header">
        <!-- 折叠图片 -->
        <div class="collapse-btn" @click="collapseChange">
            <i class="el-icon-menu"></i>
        </div>
        <div class="logo">music后台管理</div>
        <div class="header-right">
            <!-- 全屏 -->
            <div class="btn-fullscreen" @click="handleFullScreen">
                <el-tooltip :content="fullscreen? '取消全屏' : '全屏'" placement="bottom">
                    <i class="el-icon-rank"></i>
                </el-tooltip>
            </div>
            <!-- 头像 -->
            <div class="user-avator">
                <img src="../assets/img/user.jpg"/>
            </div>
            <!-- 下拉菜单 -->
            <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                <span class="el-dropdown-link">
                    {{userName}}
                    <i class="el-icon-caret-bottom"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
import bus from "../assets/js/bus";
    export default {
        name: "TheHeader",
        data(){
            return{
                collapse: false,
                fullscreen: false,
            }
        },
        computed:{
            userName(){
                return localStorage.getItem("userName");
            }
        },
        methods:{
            //侧边栏折叠
            collapseChange(){
                //发送当前侧边栏的状态
                this.collapse = !this.collapse;
                bus.$emit('collapse', this.collapse);
            },
            //全屏事件
            handleFullScreen() {
                //获取页面全部元素
                let element = document.documentElement;
                if (this.fullscreen) {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                } else {
                    if (element.requestFullscreen) {
                        element.requestFullscreen();
                    } else if (element.webkitRequestFullScreen) {
                        element.webkitRequestFullScreen();
                    } else if (element.mozRequestFullScreen) {
                        element.mozRequestFullScreen();
                    } else if (element.msRequestFullscreen) {
                        // IE11
                        element.msRequestFullscreen();
                    }
                }
                this.fullscreen = !this.fullscreen;
            },
            handleCommand(command){
                if(command === "logout"){
                    localStorage.removeItem('userName');
                    this.$router.push("/");
                }
            }
        }
    }
</script>

<style scoped>
    .header{
        position: relative;
        background-color: #253041;
        /* 带影印 有立体感 */
        box-sizing: border-box;
        width: 100%;
        height: 70px;
        font-size: 22px;
        color: #ffffff;
    }
    .collapse-btn{
        float: left;
        padding: 0 21px;
        cursor: pointer;
        line-height: 70px;
    }
    .header .logo{
        position: relative;
        float: left;
        line-height: 70px;
    }
    .header-right{
        float: right;
        padding-right: 50px;
        /* 横着 平均分配 */
        display: flex;
        height: 70px;
        align-items: center;
    }
    .btn-fullscreen{
        /* 旋转45度 */
        transform: rotate(45deg);
        margin-right: 5px;
        font-size: 24px;
    }
    .user-avator{
        margin-left: 20px;
    }
    .user-avator img{
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }
    .user-name{
        margin-left: 10px;
    }
    .el-dropdown-link{
        color: #ffffff;
        cursor: pointer;
    }
</style>